import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/utils.dart';

class EnterItem extends StatefulWidget {
  final String baseApi;
  final Map item;
  final int itemIndex;
  final String status;
  final receiveTask;

  EnterItem(
      {Key key,
      this.baseApi = 'enter/',
      this.item,
      this.itemIndex,
      this.status,
      this.receiveTask})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<EnterItem> {
  // 状态栏颜色
  Color _getHeadColor(int status, int receptorId) {
    String color = '#ffffff';
    // 待接收
    if (status == 0 && receptorId == 0) {
      color = '#FDF4F3';
    }
    // 待清点
    if (status == 0 && receptorId != 0) {
      color = '#FEFAF2';
    }
    // 已清点
    if (status == 10 && receptorId == 1) {
      color = '#F5F8FF';
    }
    return Utils.hexToColor(color);
  }

  // 状态栏状态文字转换
  String _getStatusTex(int status, String statusText, int receptorId) {
    // 待接收
    if (status == 0 && receptorId == 0) {
      return '待接收';
    } else if (status == 10 && receptorId == 1) {
      return '已清点';
    } else {
      return statusText;
    }
  }

  // 状态栏状态字体颜色
  Color _getStatusTextColor(int status, int receptorId) {
    String color = '#333333';
    // 待接收
    if (status == 0 && receptorId == 0) {
      color = '#F44745';
    }
    // 待清点
    if (status == 0 && receptorId != 0) {
      color = '#FF9132';
    }
    // 已清点
    if (status == 10 && receptorId == 1) {
      color = '#3F9DFF';
    }
    return Utils.hexToColor(color);
  }

  // 按钮颜色
  Color _getButtonColor(int status, int receptorId) {
    String color = '#dddddd';

    // 待接收
    if (status == 0 && receptorId == 0) {
      color = '#297BFF';
    }
    // 待清点
    if (status == 0 && receptorId != 0) {
      color = '#FF9131';
    }
    // 重新清点
    if (status == 11 && receptorId > 0) {
      color = '#FF9131';
    }
    // 已清点
    if (status == 10 && receptorId == 1) {
      color = '#3F9DFF';
    }
    return Utils.hexToColor(color);
  }

  // 按钮文字转换
  String _getButtonText(int status, int receptorId) {
    // 待接收
    if (status == 0 && receptorId == 0) {
      return '接受任务';
    } else {
      return '清点物资';
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    var item = widget.item;

    item['baseApi'] = widget.baseApi;

    return Container(
        width: 500.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20.w),
        ),
        margin: EdgeInsets.fromLTRB(25.w, 20.w, 25.w, 0.w),
        child: InkWell(
            onTap: () {
              Navigator.pushNamed(context, '/enter_dqd',
                  arguments: {'item': item});
            },
            onTapDown: (details) {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Column(children: <Widget>[
              Container(
                alignment: Alignment.center,
                height: 90.w,
                padding: EdgeInsets.fromLTRB(24.w, 0, 24.w, 0.w),
                decoration: BoxDecoration(
                  color: _getHeadColor(item['status'], item['receptor_id']),
                  borderRadius:
                      BorderRadius.vertical(top: Radius.circular(20.w)),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Image.asset(item['icon'] ?? 'assets/rk.png',
                            width: 40.w),
                        Container(
                          margin: EdgeInsets.only(left: 10.w),
                          child: Text(
                            '入库单号：' + item['order_no'],
                            style: TextStyle(
                                color: Color(0xFF666666),
                                fontSize: 14,
                                height: 1),
                          ),
                        )
                      ],
                    ),
                    Text(
                      _getStatusTex(item['status'], item['status_text'],
                          item['receptor_id']),
                      style: TextStyle(
                          color: _getStatusTextColor(
                              item['status'], item['receptor_id']),
                          fontSize: 14),
                    )
                  ],
                ),
              ),
              Container(
                  height: 180.w,
                  alignment: Alignment.topLeft,
                  padding: EdgeInsets.fromLTRB(24.w, 30.w, 24.w, 0.w),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start, // 左对齐
                          children: <Widget>[
                            Text(item['case_name'],
                                style: TextStyle(
                                    color: Color(0xFF333333),
                                    fontSize: 15,
                                    fontWeight: FontWeight.bold,
                                    height: 1)),
                            Container(
                              margin: EdgeInsets.only(top: 22.w),
                              child: Text('申请时间：' + item['submission_time'],
                                  style: TextStyle(
                                      color: Color(0xFF666666),
                                      fontSize: 12,
                                      height: 1)),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 13.w),
                              child: Text('申请人：' + item['user']['name'],
                                  style: TextStyle(
                                      color: Color(0xFF666666),
                                      fontSize: 12,
                                      height: 1)),
                            ),
                          ],
                        ),
                        Offstage(
                            offstage: widget.status == 'checked' ? true : false,
                            child: SizedBox(
                                width: 160.w,
                                height: 66.w,
                                child: ElevatedButton(
                                  onPressed: () {
                                    // 接受任务
                                    if (item['status'] == 0 &&
                                        item['receptor_id'] == 0) {
                                      Utils.openAlert('确定要接受任务吗？', context)
                                          .then((confirm) {
                                        if (confirm) {
                                          widget.receiveTask(item['id'],
                                              widget.itemIndex, item);
                                        }
                                      });
                                    }

                                    // 清点物资
                                    if (item['status'] == 0 &&
                                            item['receptor_id'] != 0 ||
                                        item['status'] == 11 &&
                                            item['receptor_id'] > 0) {
                                      Navigator.pushNamed(
                                          context, "/enter_check", arguments: {
                                        'id': item['id'],
                                        'itemIndex': widget.itemIndex,
                                        'baseApi': widget.baseApi
                                      });
                                    }
                                  },
                                  child: Text(
                                    _getButtonText(
                                        item['status'], item['receptor_id']),
                                    style: TextStyle(color: Colors.white),
                                  ),
                                  style: ElevatedButton.styleFrom(
                                      padding:
                                          EdgeInsets.symmetric(horizontal: 0),
                                      // color: Utils.hexToColor(item['btnColor'] ?? '#999999'),
                                      backgroundColor: _getButtonColor(
                                          item['status'], item['receptor_id']),
                                      shape: RoundedRectangleBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(8.w)))),
                                )))
                      ]))
            ])));
  }
}
